// Picture.cpp: implementation of the Picture class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AWTPicture.h"
#include <afxpriv2.h>

IMPLEMENT_DYNAMIC(AWTPicture, Image)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

AWTPicture::AWTPicture()
{
	construct(CString());
}

AWTPicture::AWTPicture(LPTSTR file) : Image(file)
{
	construct(CString(file));
}

AWTPicture::AWTPicture(CString& file) : Image(file)
{
	construct(file);
}

AWTPicture::~AWTPicture()
{
	if (m_pPicture != NULL)
		m_pPicture->Release();
}

BOOL AWTPicture::loadImage()
{
	if (m_pPicture != NULL)
	{
		m_pPicture->Release();
		m_pPicture = NULL;
	}

	if (m_file.IsEmpty())
		return FALSE;

	CFile file;
	if (!file.Open(m_file, CFile::modeRead|CFile::shareDenyWrite))
		return FALSE;

	CArchive ar(&file, CArchive::load | CArchive::bNoFlushOnDelete);
	CArchiveStream arcstream(&ar);
	
	HRESULT hr = OleLoadPicture((IStream*)&arcstream, 0, FALSE, IID_IPicture, (void**)&m_pPicture);
	file.Close();

	return (SUCCEEDED(hr));
}

CSize AWTPicture::getSize()
{
	if (!m_pPicture)
		return CSize(0,0);
	
	LONG hmWidth, hmHeight; // HIMETRIC units
	m_pPicture->get_Width(&hmWidth);
	m_pPicture->get_Height(&hmHeight);
	CSize sz(hmWidth,hmHeight);
	CWindowDC dc(NULL);
	dc.HIMETRICtoDP(&sz); // convert to pixels
	return sz;
}

void AWTPicture::paint(CDC* pDC, AWTCanvas* canvas)
{
	if (m_pPicture == NULL)
		return;

	CRect rc;
	canvas->getPeer()->GetClientRect(&rc);

	long cx, cy;
	m_pPicture->get_Width(&cx);
	m_pPicture->get_Height(&cy);

	m_pPicture->Render(*pDC, rc.left, rc.top, rc.Width(), rc.Height(),
		0, cy, cx, -cy, &rc);
}



